package org.bao.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bao.bean.phoneBean;
import org.bao.bean.shoppingcarBean;
import org.bao.service.IPhoneMsg;
import org.bao.service.IshoppingCar;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

/**
 * @author 购物车Action
 *
 */
@Controller
public class ShoppingCarAction {

	@Resource
	private IshoppingCar CarS;// 按名称匹配进行注入,购物车Service
	private IPhoneMsg proS;// 按名称匹配进行注入,商品Service

	/**
	 * 显示购物车数据
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("QueryshopppingCar")
	public List<?> telAjaxQuery(HttpServletRequest request) {
		String strphoneId = "";
		String strphoneNum = "";

		// 获取用户id
		Integer userid = (Integer) request.getSession().getAttribute("userid");
		System.out.println("用户ID:" + userid);

		// 只需要用户id就可以查出用户购物车信息
		List<?> queryList = CarS.Queryphone(userid);
		
//		Iterator it = queryList.iterator();
//		while (it.hasNext()) {
//			shoppingcarBean CarB = (shoppingcarBean) it.next();
//
//			System.out.println(CarB);
//
//			strphoneId = strphoneId + CarB.getShoppid() + ",";
//			strphoneNum = strphoneNum + CarB.getShoppnum() + ",";
//		}
//		// 去除最后一个,
//		strphoneId = strphoneId.substring(0, strphoneId.length() - 1);
//		strphoneNum = strphoneNum.substring(0, strphoneNum.length() - 1);
//
//		System.out.println("phoneid:" + strphoneId);
//		System.out.println("phoneNum:" + strphoneNum);
//
//		// 根据ID来查找商品信息
//		List<phoneBean> lisphoneMsg = proS.selectIdListInfo(strphoneId);

		return queryList;
	}

	/**
	 * 添加商品到购物车表
	 * 
	 * @param request
	 */

	@RequestMapping("addShoppingCar")
	public void addshoppingCar(HttpServletRequest request, HttpServletResponse response) {
		int phoneidI = 0;
		// 前台收到的数据
		String phoneid = request.getParameter("phoneid");
		// 获取用户id
		Integer userid = (Integer) request.getSession().getAttribute("userid");
		// 类型转换
		phoneidI = Integer.parseInt(phoneid);

		System.out.println("商品ID:" + phoneid);
		System.out.println("用户ID:" + userid);

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}

		if (phoneid != null && userid != null) {

			shoppingcarBean shoCarB = new shoppingcarBean(userid, phoneidI);

			int count = CarS.insertShoCar(shoCarB);

			if (count > 0) {
				out.write("1");
			} else {
				out.write("0");
			}

		} else {
			out.write("parent.layer.msg('请先登陆', {icon : 4});");
		}

	}

}
